﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Routing;
using System.Web.UI;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;
using phudieuhungthinh.Helpers;

namespace phudieuhungthinh.Pages
{
    public partial class ProductPage : Page, IRoutablePage
    {
        protected RequestContext requestContext;

        public int ProductID
        {
            get
            {
                Int32 result = 0;
                return (Int32.TryParse(Request.QueryString["id"], out result)) ? result : 0;
            }
        }

        public string Url
        {
            get { return RouteValue("url").ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id;
                pProduct.Controls.Add(new LiteralControl(LayoutProduct(out id)));
                pSlide.Controls.Add(new LiteralControl(LayoutSlide(id)));
            }
        }

        protected string LayoutProduct(out int productId)
        {
            try
            {
                var html = new StringBuilder();
                var im = new ProductImpl();
                ProductModel item = im.GetProductInfo(Url);

                html.AppendFormat("<div class='title'><span>{0}</span><span> ({1})</span></div>", item.Name,
                    item.ProductNumber);
                html.Append("<div class='img'>");
                if (item.ImageUrl != null)
                {
                    // /Images/Products/Copyright/
                    html.AppendFormat("<img src='/hinh-anh/dieu-khac/{0}' alt='{1}' />", item.Url, item.Name);
                }
                html.Append("</div>");
                html.AppendFormat("<div class='description'>{0}</div>", item.Description);
                productId = item.ID;
                return html.ToString();
            }
            catch
            {
                productId = -1;
                Response.Redirect("/loi-404");
                return string.Empty;
            }
        }

        protected string LayoutSlide(int pId)
        {
            try
            {
                var im = new ProductImpl();
                List<ProductModel> items = im.GetProductExcept(pId);
                if (items.Count > 0)
                {
                    var html =
                        new StringBuilder(
                            "<div class='slideshow' data-cycle-auto-height='3:1' data-cycle-fx='carousel' data-cycle-timeout='3000' data-cycle-timeout='0' data-cycle-carousel-visible='4' data-cycle-slides='> div' data-cycle-next='#next' data-cycle-prev='#prev' data-cycle-pager='#pager'>");

                    for (int i = 0; i < items.Count && i < 20; i++)
                    {
                        html.Append("<div class='item'>");
                        html.AppendFormat("<a href='/san-pham/{0}' class='overlay'></a>", items[i].Url);
                        html.Append("<div class='img'>");
                        if (items[i].ImageUrl != null)
                        {
                            html.AppendFormat("<img src='/hinh-anh/phu-dieu/{0}' alt='{1}' />", items[i].Url,
                                items[i].Name);
                        }
                        html.Append("</div>");
                        //html.AppendFormat("<div class='title'><span>{0}</span><span> ({1})</span></div>", items[i].Name, items[i].ProductNumber);
                        html.Append("</div>");
                    }

                    html.Append("</div>");
                    html.Append("<div class='center'>");
                    html.Append("<a href='#' id='prev' class='nav'></a><a href='#' id='next' class='nav'></a>");
                    html.Append("</div>");
                    html.Append("<div class='cycle-pager' id='pager'>");
                    html.Append("</div>");

                    return html.ToString();
                }
                return string.Empty;
            }
            catch
            {
                Response.Redirect("/loi-404");
                return string.Empty;
            }
        }


        protected object RouteValue(string key)
        {
            return requestContext.RouteData.Values[key];
        }

        #region IRoutablePage Members

        public RequestContext RequestContext
        {
            set { requestContext = value; }
        }

        #endregion
    }
}